package tiny.soft.com.common.bean;


import jdk.internal.dynalink.beans.StaticClass;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
public class ResultBean {
    private int code = 200;
    private String requestId;
    private Map<String,Object> data = new HashMap<>();
    private List<Error> errors = null;
    private boolean serviceSuccess;
    private String redirectUrl;

    public  static ResultBean setBean(Object obj){
        ResultBean resultBean=new ResultBean();
        resultBean.addField("data",obj);
        return resultBean;
    }

    public ResultBean() {
        this.serviceSuccess = Boolean.TRUE;
    }

    public ResultBean addField(String field, Object value) {
        if (field != null) {
            this.data.put(field, value);
        }
        return this;
    }
    public ResultBean success() {
        return this;
    }

    public ResultBean error(){
        this.serviceSuccess=Boolean.FALSE;
        return this;
    }
    public ResultBean setError(String msg){
        this.addErrors("1",msg);
        return this;
    }
    public ResultBean addErrors(String errorCode, String msg) {
        if (this.errors == null) {
            error();
            this.errors = new ArrayList();
        }
        this.errors.add(new Error(errorCode, msg));
        return this;
    }
    public ResultBean addErrors(String errorCode, String paramter, String msg) {
        if (this.errors == null) {
            error();
            this.errors = new ArrayList();
        }
        this.errors.add(new Error(errorCode, paramter, msg));
        return this;
    }
}